/*
 *    Copyright (c) 2018-2025, boss All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the boss developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: boss
 */
package com.hopes.bizz.poll.monitor.service.impl;

import cn.hutool.json.JSONArray;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.hopes.bizz.poll.monitor.dto.PollTemplateDTO;
import com.hopes.bizz.poll.monitor.entity.PollMonitorFactorEntity;
import com.hopes.bizz.poll.monitor.entity.PollTemplateEntity;
import com.hopes.bizz.poll.monitor.mapper.PollTemplateMapper;
import com.hopes.bizz.poll.monitor.service.PollMonitorFactorService;
import com.hopes.bizz.poll.monitor.service.PollTemplateService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.List;

/**
 * 模板表
 *
 * @author yumr
 * @date 2023-05-24 14:22:47
 */
@Service
@RequiredArgsConstructor
public class PollTemplateServiceImpl extends ServiceImpl<PollTemplateMapper, PollTemplateEntity> implements PollTemplateService {

    private final PollMonitorFactorService pollMonitorFactorService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean save(PollTemplateEntity entity) {
        Assert.hasText(entity.getName(), "template name cannot be empty!");
        baseMapper.delete(Wrappers.<PollTemplateEntity>lambdaQuery().eq(PollTemplateEntity::getName, entity.getName()));
        return SqlHelper.retBool(getBaseMapper().insert(entity));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveMonitorSiteFactor(PollTemplateDTO templateDTO) {
        Assert.notNull(templateDTO.getId(), "template id cannot be null");
        Assert.hasText(templateDTO.getMonitorSiteId(), "monitor site id cannot be empty!");
        final PollTemplateEntity templateEntity = baseMapper.selectById(templateDTO.getId());
        if (null == templateEntity || CollectionUtils.isEmpty(templateEntity.getContent())) {
            return false;
        }

        final LambdaQueryWrapper<PollMonitorFactorEntity> wrapper = Wrappers.
                <PollMonitorFactorEntity>lambdaQuery().eq(PollMonitorFactorEntity::getMonitorSiteId, Long.valueOf(templateDTO.getMonitorSiteId()));
        pollMonitorFactorService.remove(wrapper);

        final JSONArray content = templateEntity.getContent();
        final List<PollMonitorFactorEntity> pollMonitorFactorEntities = JSON.parseArray(JSON.toJSONString(content), PollMonitorFactorEntity.class);
        pollMonitorFactorEntities.forEach(item -> {
            item.setId(null);
            item.setMonitorSiteId(Long.valueOf(templateDTO.getMonitorSiteId()));
        });
        return pollMonitorFactorService.saveBatch(pollMonitorFactorEntities);
    }
}
